const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

// api
router.get('/product/detail/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

router.get('/customer/product/view/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

router.get('/customer/product/edit/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

// 产品灰度 本地测试用 productV2  打包 上线需改为product
const productGray = 'productV2'

/** start 产品 */
router.get('/customer/product', async ctx => {
  // let script = ['/product.list.js'];
  // let modConfig = modules['product.list'];
  let script = [`/${productGray}.list.js`];
  let modConfig = modules[`${productGray}.list`];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/customer/product', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('产品管理', body, script, modConfig.template, ctx)
});

router.get('/customer/product/view/:id', async ctx => {
  // let script = ['/product.view.js'];
  // let modConfig = modules['product.view'];
  let script = [`/${productGray}.view.js`];
  let modConfig = modules[`${productGray}.view`];
  let reqHeaders = ctx.request.headers;
  
  let result = await HttpClient.request(`/customer/product/view/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('产品详情', body, script, modConfig.template, ctx)
});

router.get('/customer/product/create', async ctx => {
  
  // let modConfig = modules['product.edit'];
  // let script = ['/product.edit.js'];

  let script = [`/${productGray}.edit.js`];
  let modConfig = modules[`${productGray}.edit`];

  
  let reqHeaders = ctx.request.headers;
  
  const customerId = ctx.request.query.customerId;
  
  let result = await HttpClient.request(`/customer/product/create?customerId=${customerId}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('新建产品', body, script, modConfig.template, ctx)
});

router.get('/customer/product/edit/:id', async ctx => {
  // let modConfig = modules['product.edit'];
  // let script = ['/product.edit.js'];
  let script = [`/${productGray}.edit.js`];
  let modConfig = modules[`${productGray}.edit`];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(`/customer/product/edit/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('编辑产品', body, script, modConfig.template, ctx)
});

router.get('/customer/product/register/create/:registerId', async ctx => {
  let script = [`/${productGray}.edit.js`];
  let modConfig = modules[`${productGray}.edit`];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(`/customer/product/register/create/${ctx.params.registerId}`, 'get', null, {headers: reqHeaders});
  let body = result.body;
  ctx.body = await Template.renderWithHtml('新建产品', body, script, modConfig.template, ctx)
});

router.get('/customer/product/register/edit/:id/:registerId', async ctx => {
  let script = [`/${productGray}.edit.js`];
  let modConfig = modules[`${productGray}.edit`];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request(`/customer/product/register/edit/${ctx.params.id}/${ctx.params.registerId}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('编辑产品', body, script, modConfig.template, ctx)
});

router.get('/product/createOnTask', async ctx => {
  // let modConfig = modules['product.edit'];
  // let script = ['/product.edit.js'];
  let script = [`/${productGray}.edit.js`];
  let modConfig = modules[`${productGray}.edit`];
  let reqHeaders = ctx.request.headers;
  // let result = await HttpClient.request(`/customer/product/edit/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let result = await HttpClient.request('/product/createOnTask', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('编辑产品', body, script, modConfig.template, ctx)
});

router.get('/product/createOnEvent', async ctx => {
  // let modConfig = modules['product.edit.modal'];
  // let script = ['/product.edit.modal.js'];
  let script = [`/${productGray}.edit.modal.js`];
  let modConfig = modules[`${productGray}.edit.modal`];
  
  let reqHeaders = ctx.request.headers;
  // let result = await HttpClient.request(`/customer/product/edit/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let result = await HttpClient.request('/product/createOnTask', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('编辑产品', body, script, modConfig.template, ctx)
});

/** end 产品 */

/** start 产品模板 */
router.get('/product', async ctx => {
  let script = ['/product.template.list.js'];
  let modConfig = modules['product.template.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/product', 'get', null, {headers: reqHeaders});
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('产品模板列表', body, script, modConfig.template, ctx)
});

router.get('/product/create', async ctx => {
  let modConfig = modules['product.template.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/product.template.edit.js'];
  let result = await HttpClient.request('/product/create', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('新建产品模板', body, script, modConfig.template, ctx)
});

router.get('/product/edit/:id', async ctx => {
  let modConfig = modules['product.template.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/product.template.edit.js'];
  let result = await HttpClient.request(`/product/edit/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('编辑产品模板', body, script, modConfig.template, ctx)
});

router.get('/product/detail/:id', async ctx => {
  let modConfig = modules['product.template.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/product.template.view.js'];
  let result = await HttpClient.request(`/product/detail/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('产品模板信息', body, script, modConfig.template, ctx)
});
/** end 产品模板 */

/** start 产品注册 */
router.get('/product/register', async ctx => {
  let script = ['/product.register.list.js'];
  let modConfig = modules['product.register.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/product/register', 'get', null, {headers: reqHeaders});
  let body = result.body;
  
  ctx.body = await Template.renderWithHtml('产品注册列表', body, script, modConfig.template, ctx)
});

router.get('/product/register/create', async ctx => {
  let modConfig = modules['product.register.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/product.register.edit.js'];
  let result = await HttpClient.request('/product/register/create', 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('新建产品注册', body, script, modConfig.template, ctx)
});

router.get('/product/register/edit/:id', async ctx => {
  let modConfig = modules['product.register.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/product.register.edit.js'];
  let result = await HttpClient.request(`/product/register/edit/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('编辑产品注册', body, script, modConfig.template, ctx)
});

// 与下面路由冲突，产品注册页面打不开，单独加下
router.get('/product/register/detail/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

router.get('/product/register/detail/:id', async ctx => {
  let modConfig = modules['product.register.detail'];
  let reqHeaders = ctx.request.headers;
  let script = ['/product.register.detail.js'];
  let result = await HttpClient.request(`/product/register/detail/${ctx.params.id}`, 'get', null, {headers: reqHeaders});
  let body = result.body;

  ctx.body = await Template.renderWithHtml('产品注册信息', body, script, modConfig.template, ctx)
});
/** end 产品注册 */

module.exports = router;
